#include <func.h>

char* simplifyPath(char* path){
    unsigned int len = strlen(path);
    char *ret = (char *)malloc(sizeof(char) * (len + 1));
    memset(ret, 0, sizeof(char) * (len + 1));
    int top = 0;
    ret[top++] = '/';
    for(int i=0; i<len; i++){
        if(path[i] == '/' && ret[top - 1] == '/'){
            continue;
        }
        if(path[i] == '.'){
            int cnt = 0;
            while(path[i] == '.'){
                cnt++;
                i++;
            }
            i--;
            if(cnt > 2){
                for(int j=0; j<cnt; j++){
                    ret[top++] = '.';
                }
            }else if(cnt == 2){
                if((i+1 < len && path[i+1] != '/') || (i > 1 &&path[i-2] != '/')){
                    ret[top++] = '.';
                    ret[top++] = '.';
                }else if(top > 1){
                    top--;
                    while(ret[top -1] != '/'){
                        top--;
                    }
                }
            }else if((cnt == 1 && i+1 < len && path[i+1] != '/')|| path[i-1] != '/'){
                ret[top++] = '.';
            }
            continue;
        }
        ret[top++] = path[i];
    }
    while(ret[top - 1] == '/' && top > 1){
        top--;
    }
    ret[top] = '\0';
    return ret;
}

int myLs(char*dirPath,char* username){
    int i = 0;
    char checkPath[256];
    int preId,flag1,flag2;
    strcpy(checkPath,simplifyPath(dirPath));
    printf("checkPath = %s\n",checkPath);

    //数据库中查询信息
    MYSQL * mysql = mysql_init(NULL); // 分配内存 初始化
    MYSQL * ret = mysql_real_connect(
                                     mysql,
                                     "localhost",
                                     "root",
                                     "123123",
                                     "netdisk",
                                     0,NULL,0
                                    );
    if(ret == NULL){
        fprintf(stderr,"mysql_real_connect:%s\n", mysql_error(mysql));
        return -1;
    }
    char sql[1024] = "select * from fileinfo;";
    int qret = mysql_query(mysql,sql);
    if(qret != 0){
        fprintf(stderr,"mysql_query:%s\n", mysql_error(mysql));
        return -1;
    }
    MYSQL_RES *res = mysql_store_result(mysql);
    
    while(1){
        MYSQL_ROW row = mysql_fetch_row(res);//取出一行
        if(row == NULL){
            break;
        }
        for(unsigned j = 0; j < mysql_num_fields(res); ++j){
            if(strcmp(checkPath,row[j]) == 0){
                flag1 = 1;
            }
            if(strcmp(username,row[j])){
                flag2 = 1;
            }
        }
        if(flag1 == 1 && flag2 == 1){
            preId = atoi(row[0]);
            break;
        }
        flag1 = 0;
        flag2 = 0;
    }
    mysql_free_result(res);

    strcpy(sql,"select * from fileinfo;");
    qret = mysql_query(mysql,sql);
    if(qret != 0){
        fprintf(stderr,"mysql_query:%s\n", mysql_error(mysql));
        return -1;
    }
    res = mysql_store_result(mysql);
    while(1){
        MYSQL_ROW row = mysql_fetch_row(res);//取出一行
        if(row == NULL){
            break;
        }
        if(atoi(row[3]) == preId ){
            printf("%s  ",row[1]);
            i++;
            if(i % 7 == 0){
                printf("\n");
            }
        }
    }
    printf("\n");
    mysql_free_result(res);
    mysql_close(mysql); // 回收资源
}

int main(int argc,char* argv[])
{
    char dirPath[] = "/";
    char username[] = "zhangsan";
    myLs(dirPath,username);   
    return 0;
}

